/*
 * SimpleDate.java
 *
 * $Id$
 *
 */

package jm.lib.common;

import java.io.Serializable;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import jm.lib.util.DateUtil;

/**
 *
 * @author Jiming Liu
 */
public class SimpleDate extends GregorianCalendar
        implements Cloneable, Serializable{
    private static final long serialVersionUID = 7360232935893870819L;
    
    public SimpleDate() {
        super();
        this.setTimeInMillis(DateUtil.currentTimeMillis());
    }
    
    public SimpleDate(Calendar c) {
        super();
        this.setTimeInMillis(c.getTimeInMillis());
    }
    
    public SimpleDate(java.util.Date date) {
        super();
        this.setTimeInMillis(date.getTime());
    }
    
    public SimpleDate(long millis) {
        super();
        this.setTimeInMillis(millis);
    }
    
    public SimpleDate(int year, int month, int day){
        this(year, month, day, 0, 0, 0);
    }
    
    public SimpleDate(int year, int month, int day, int hour, int minute, int second){
        super();
        this.set(year, month, day, hour, minute, second);
        this.setMilliSecondPart(0);
    }
    
    public int getYear(){
        return get(Calendar.YEAR);
    }
    
    public void setYear(int year){
        set(Calendar.YEAR, year);
    }
    
    
    
    public int getMonth(){
        return get(Calendar.MONTH);
    }
    
    public void setMonth(int month){
        set(Calendar.MONTH, month);
    }
    
    public int getDayFrom1970() {
    	long time = this.getTimeInMillis() + this.getTimeZone().getRawOffset();
    	return (int)((time) / DateUtil.MS_IN_A_DAY + (time<0?-1:1));
    }
    
    public int getDayOfMonth(){
        return get(Calendar.DAY_OF_MONTH);
    }
    
    public void setDayOfMonth(int dayOfMonth){
        set(Calendar.DAY_OF_MONTH, dayOfMonth);
    }
    
    public int getDayOfWeek(){
        return get(Calendar.DAY_OF_WEEK);
    }
    
    public int getDayOfYear(){
        return get(Calendar.DAY_OF_YEAR);
    }
    
    public int getHour(){
        return get(Calendar.HOUR_OF_DAY);
    }
    
    public void setHour(int hour){
        set(Calendar.HOUR_OF_DAY, hour);
    }
    
    public int getMinute(){
        return get(Calendar.MINUTE);
    }
    
    public void setMinute(int minute){
        set(Calendar.MINUTE, minute);
    }
    
    public int getSecond(){
        return get(Calendar.SECOND);
    }
    
    public void setSecond(int second){
        set(Calendar.SECOND, second);
    }
    
    public int getMilliSecondPart(){
        return get(Calendar.MILLISECOND);
    }
    
    public void setMilliSecondPart(int millis){
        set(Calendar.MILLISECOND, millis);
    }
    
    public void addYears(int amount){
        add(Calendar.YEAR, amount);
    }
    
    public void addMonths(int amount){
    	add(Calendar.MONTH, amount);
    }
    
    public void addWeeks(int amount){
    	add(Calendar.WEEK_OF_YEAR, amount);
    }
    
    public void addDays(int amount){
    	add(Calendar.DATE, amount);
    }
    
    public void addHours(int amount){
    	add(Calendar.HOUR_OF_DAY, amount);
    }
    
    public void addMinutes(int amount){
    	add(Calendar.MINUTE, amount);
    }
    
    public void addSeconds(int amount){
    	add(Calendar.SECOND, amount);
    }   
    
    public Date toDate(){
        return getTime();
    }

	/**
	 * Returns <code>true</code> if this <code>SimpleDate</code> is the same as the o argument.
	 *
	 * @return <code>true</code> if this <code>SimpleDate</code> is the same as the o argument.
	 */
	public boolean equals(Object o) {
		if (this == o) {
			return true;
		}
		if (!super.equals(o)) {
			return false;
		}
		if (o == null) {
			return false;
		}
		if (o.getClass() != getClass()) {
			return false;
		}
		return true;
	}

	/**
	 * Override hashCode.
	 *
	 * @return the Objects hashcode.
	 */
	public int hashCode() {
		int hashCode = super.hashCode();
		hashCode = 31
			* hashCode
			+ (int) (+serialVersionUID ^ (serialVersionUID >>> 32));
		return hashCode;
	}


}
